﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AudioMgr : MonoBehaviour {
	static GameObject go;
	static AudioMgr adMgr;
	//音乐文件
	public AudioSource music;
	public AudioSource sound;
	//slider
	public Slider musicSld;
	public Slider soundSld;

	public static AudioMgr getInstance() {
		if (adMgr == null) {
			adMgr = go.GetComponent<AudioMgr> ();
		}
		return adMgr;
	}

	void Awake() {
		go = gameObject;
		//设置默认音量
		music.volume = 0.3F;
		sound.volume = 0.7F;
		initSldEvent ();
	}

	void initSldEvent(){
		musicSld.value = music.volume;
		musicSld.onValueChanged.AddListener (delegate {
			music.volume = musicSld.value;
		});
		soundSld.value = sound.volume;
		soundSld.onValueChanged.AddListener (delegate {
			sound.volume = soundSld.value;
		});
	}

//	void OnGUI() {
//		int iMusicY = Screen.height - 80;
//		int iSoundY = Screen.height - 160;
//		GUIStyle fontStyle=new GUIStyle();  
//		fontStyle.normal.background = null;
//		fontStyle.normal.textColor=Color.white;
//		fontStyle.fontSize = 30;
//
//		GUI.Label(new Rect(30, iMusicY - 15, 100, 20), "music:", fontStyle);
//		GUI.Label(new Rect(30, iSoundY - 15, 100, 20), "sound:", fontStyle);
//
//		musicVolume = GUI.HorizontalSlider (new Rect(140, iMusicY, 200, 50), musicVolume, 0.0F, 1.0F);
//		if (music.isPlaying){
//			//音乐播放中设置音乐音量 取值范围 0.0F到 1.0
//			music.volume = musicVolume;
//		}
//		soundVolume = GUI.HorizontalSlider (new Rect(140, iSoundY, 200, 50), soundVolume, 0.0F, 1.0F);
//		if (sound.isPlaying){
//			//音乐播放中设置音乐音量 取值范围 0.0F到 1.0
//			sound.volume = soundVolume;
//		}
//	}

	public void PlaySound(string soundName){
		AudioClip clip = Resources.Load ("audio/" + soundName) as AudioClip;
		sound.PlayOneShot(clip);
	}

	void stopSound(){
		sound.Stop ();
	}
}
